
#ifndef __RSA_PRIVATE_KEY_H
#define __RSA_PRIVATE_KEY_H

#include "PrivateKey.h"
#include "DecryptionKey.h"
#include "SigningKey.h"
#include <openssl/rsa.h>
#include "../iface/SSLInterface.h"

/** An class encapsulating an RSA private key. */
class RSAPrivateKey : public PrivateKey, public DecryptionKey, public SigningKey
{
   public:
      RSAPrivateKey();
      RSAPrivateKey(const char *name);
      // Construct a duplicate of the private key
      RSAPrivateKey(const RSAPrivateKey &other);
      // Construct from PEM format
      RSAPrivateKey(const char *pemtext, const char *name);
      // Construct from encrypted PEM format
      RSAPrivateKey(const char *pemtext, const char *password, const char *name);
      // Construct from OpenSSL format
      RSAPrivateKey(RSA *prvkey, const char *name);
      // Assignment constructor
      RSAPrivateKey &operator=(const RSAPrivateKey &other);
      // Destroy RSA private key
      ~RSAPrivateKey();
      // Get new OpenSSL RSA object
      int getSSLKeyComponents(RSA **prvkey) const;
      // Get PEM data
      int getPEMKeyData(char **pemtext) const;
      // Get encrypted PEM data
      int getPEMKeyData(char **pemtext, const char *password) const;
      // Compare key to key
      int compareKeyBelongs(PublicKey *pubkey) const;
      // Get key type name
      static const keytypetype getStaticKeyType() { return KEY_TYPE_PRIVATE_RSA; }
      const keytypetype getKeyType() const { return KEY_TYPE_PRIVATE_RSA; }
      // To import from text - Base64 RAW or PEM
      int importKeyFromASCII(const char *text, const char *name);
      // To export to text - Base64 RAW or PEM
      int exportKeyToASCII(char **text) const;
      // Import from text with password
      int importKeyFromASCIIWithPassword(const char *text, const char *password, const char *name);
      // Export to text with password
      int exportKeyToASCIIWithPassword(char **text, const char *password) const;
   private:
      // Copy SSL type public keys
      static int copyRSASSLPrivateKey(RSA *dst, RSA *src);
      // Import private key (with optional password if it's encrypted)
      int importPrivateKeyWithOptionalPassword(const char *pemtext, const char *password);
      /** A pointer to the real RSA private key, held in SSL
       * format. */
      RSA *key;
};

#endif
